from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relation
from divirt import sqlalchemy_meta_data
from divirt import logging
from vm import Vm

log = logging.getLogger('divirt.components.nic')

Base = declarative_base(metadata=sqlalchemy_meta_data)
class Nic(Base):
    # Table
    __tablename__ = 'nics'
    id = Column(Integer, primary_key=True)
    vm_id = Column(Integer, ForeignKey('vms.id'))
    mac_addr = Column(String)
    
    # Relations
    vm = relation(Vm, backref=backref('nics', order_by=id))

    def __init__(self, vm_id=None, mac_addr=None):
        self.vm_id = vm_id
        self.mac_addr = mac_addr

    
    def __repr__(self):
        return '<Nic(id=%s, vm_id=%s, mac_addr=%s)>' \
            % (self.id, self.vm_id, self.mac_addr)
